package de.metalcon.gwt.server;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

import javax.servlet.ServletContext;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.metalcon.gwt.client.rpc.ContentService;
import de.metalcon.gwt.server.modules.AModuleServer;
import de.metalcon.gwt.shared.ContentTransmitter;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class ContentServiceImpl extends RemoteServiceServlet implements
		ContentService {

	@Override
	public ContentTransmitter getContentTransmitter(String moduleName,
			HashMap<String, Object> requestVars)
			throws IllegalArgumentException {
		try {
			AModuleServer module;
			Class moduleDef;
			Class[] intArgsClass = new Class[] { HashMap.class,
					ServletContext.class };
			Constructor moduleConstructor;

			moduleDef = Class
					.forName(de.metalcon.gwt.server.modules.AModuleServer.class
							.getPackage().getName() + "." + moduleName+"Server");
			moduleConstructor = moduleDef.getConstructor(intArgsClass);
			module = (AModuleServer) moduleConstructor.newInstance(requestVars,
					getServletContext());
			return module.getTransmitter();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
}
